<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/1
 * Time: 22:15
 */

namespace common\models\Service;


use common\models\Cache;
use common\models\CommonFunction;
use common\models\DAO\HongbaoSetting;
use common\models\DAO\UserHongbao;
use common\models\DAO\UserHongbaoList;
use common\models\DAO\UserHongbaoUseList;

class HongBaoService {

    /*
     * 用户红包总数
     */
    public static function get($userID){
        $money = 0;
        $result = UserHongbao::findOne($userID);
        if($result)
            $money = intval($result['money']);
        return $money;
    }

    /*
     * 用户使用的红包总数
     */
    public static function useInfo($userID){
        $money = 0;
        $result = UserHongbaoUseList::find()->where(['userID'=>$userID])->sum('hongBao');
        if($result)
            $money = $result;
        return $money;
    }

    /*
     * 用户领取红包数量
     */
    public static function total($userID){
        $money = 0;
        $result = UserHongbaoList::find()->where(['userID'=>$userID])->sum('money');
        if($result)
            $money = $result;
        return $money;
    }

    /*
     * 红包设置
     */
    public static function setting($day){
        $cache_name = 'hongbao_scale';
        $cache = Cache::get($cache_name);
        if(!$cache){
            $setting = HongbaoSetting::find()->orderBy('day desc')->asArray()->all();
            $cache = [];
            foreach($setting as $v){
                $cache[$v['day']] = $v['scale'];
            }
            Cache::set($cache_name, $cache);
        }
        $scale = $cache[$day];
        return intval($scale)/10;
    }

    /*
     * 更新用户红包
     */
    public static function update($userID, $money, $relateID, $type = 0){
        $money = intval($money);
        $user_money = 0;
        $hongBao = UserHongbao::findOne($userID);
        if($hongBao)
            $user_money = intval($hongBao['money']);
        else
            return false;
        if($user_money < $money)
            return false;
        $now = time();
        $hongBao->money = $user_money - $money;
        $hongBao->updateTime = $now;
        $hb_result = $hongBao->save();
        if(!$hb_result)
            return false;
        $hb_use = new UserHongbaoUseList();
        $hb_use->userID = $userID;
        $hb_use->money = $money;
        $hb_use->createTime = $now;
        $hb_use->investID = $relateID;
        $hb_use->type = $type;
        $hb_use_result = $hb_use->save();
        return $hb_use_result;
    }

    /*
     * 列表添加数据
     */
    public static function addList($param){
        $userID = $param['userID'];
        $money = $param['money'];
        $now = $param['createTime'];
        $relateID = $param['investID'];
        $type = $param['type'];
        $hb_use = new UserHongbaoUseList();
        $hb_use->userID = $userID;
        $hb_use->money = $money;
        $hb_use->createTime = $now;
        $hb_use->userInvestID = $relateID;
        $hb_use->type = $type;
        $hb_use_result = $hb_use->save();
        return $hb_use_result;
    }

    /*
     * 红包统计
     */
    public static function statistics($userID){
        $result['can'] = CommonFunction::formatMoney(self::get($userID));
        $result['total'] = CommonFunction::formatMoney(self::total($userID));
        $result['use'] = CommonFunction::formatMoney(self::useInfo($userID));
		//红包获取记录数量
		$result['incomeNum']=UserHongbaoList::find()->where(['userID'=>$userID])->count();
		//红包使用记录数量
		$result['useNum']=UserHongbaoUseList::find()->with('invest')->where(['userID'=>$userID])->count();
        return $result;
    }
	
	
    /*
     * 红包获取列表
     */
    public static function getList($userID, $page = 1, $limit = 10){
        if($page <= 0)
            $page = 1;
        if($limit <= 0)
            $limit = 10;
        $start = (intval($page) - 1) * $limit;
        $list = UserHongbaoList::find()->where(['userID'=>$userID])->asArray()->limit($limit)->offset($start)->orderBy('id desc')->all();
        $result = [];
        if($list){
            foreach($list as $v){
                $data['time'] = date('Y-m-d', $v['createTime']);
                if(!empty($v['haoBaoID'])){
                    $data['name'] = '积分兑换';
                }else if(!empty($v['lotteryID'])){
                    $data['name'] = '抽奖';
                }else if(!empty($v['sendID'])){
                    $data['name'] = '好友红包';
                }else if($v['gameID'] == 1){
                    $data['name'] = '撑杆游戏通关';
                }else if(!empty($v['backID'])){
                    $data['name'] = '红包退回';
                }
                else if($v['followID'] == 2){
                    $data['name'] = '系统补偿';
                }else{
                    $data['name'] = '首次关注微信';
                }
                $data['money'] = CommonFunction::formatMoney($v['money']);
                $result[] = $data;
            }
        }
        return $result;
    }

    /*
     * 红包消费列表
     */
    public static function getUseList($userID, $page = 1, $limit = 10){
        if($page <= 0)
            $page = 1;
        if($limit <= 0)
            $limit = 10;
        $start = (intval($page) - 1) * $limit;
        $list = UserHongbaoUseList::find()->where(['userID'=>$userID])->limit($limit)->offset($start)->asArray()->orderBy('id desc')->all();
        $result = [];
        foreach($list as $v){
            $data['time'] = date('Y-m-d', $v['createTime']);
            $data['money'] = CommonFunction::formatMoney($v['hongBao']);
            $type = $v['type'];
            $id = $v['investID'];
            $name = '散标';
            if($type == 1)  $name = DayService::get($id)['name'];
            else if($type == 2) $name = '发红包';
            $data['name'] = $name;
            $result[] = $data;
        }
        return $result  ;
    }

    public static function addHongbao($param){
        $userID = $param['userID'];
        $now = time();
        $hongBao = $param['money'];
        $user_hongbao = UserHongbao::findOne($userID);
        if($user_hongbao){
            $user_hongbao->money = $user_hongbao['money'] + $hongBao;
            $user_hongbao->updateTime = $now;
        }else{
            $user_hongbao = new UserHongbao();
            $user_hongbao->money = $hongBao;
            $user_hongbao->updateTime = $now;
            $user_hongbao->userID = $userID;
        }
        $hongbao_result = $user_hongbao->save();
        if($hongbao_result){
            $user_hongbao_list = new UserHongbaoList();
            $user_hongbao_list->userID = $userID;
            $user_hongbao_list->money = $hongBao;
            $user_hongbao_list->createTime = $now;
            $user_hongbao_list->haoBaoID = 0;
            $user_hongbao_list->gameID = 0;
            $user_hongbao_list->sendID = 0;
            $user_hongbao_list->followID = 2;

            $user_hongbao_list_result = $user_hongbao_list->save();
            if($user_hongbao_list_result){
                if(!empty($param['openID'])){
                    $_hongBao = CommonFunction::formatMoney($hongBao);
                  /*  $content = "恭喜您，获得{$_hongBao}元现金红包（微信首次关注专享）";
                    $desc && $content = $desc;*/
                    SendWxMessage::hongbao($param['openID'], $_hongBao.'元', $param['desc']);
                }
                return true;
            }else
                return false;
        }else
            return $hongbao_result;

    }


    /*
     * 获得红包
     */
    public static function add($userInfo, $hongBao, $hongBaoID = 0, $game = 0, $desc = '',$sendID = 0){

        $userID = $userInfo['userID'];

        $now = time();
        $user_hongbao = UserHongbao::findOne($userID);
        if($user_hongbao){
            $user_hongbao->money = $user_hongbao['money'] + $hongBao;
            $user_hongbao->updateTime = $now;
        }else{
            $user_hongbao = new UserHongbao();
            $user_hongbao->money = $hongBao;
            $user_hongbao->updateTime = $now;
            $user_hongbao->userID = $userID;
        }
        $hongbao_result = $user_hongbao->save();
        if($hongbao_result){
            $user_hongbao_list = new UserHongbaoList();
            $user_hongbao_list->userID = $userID;
            $user_hongbao_list->money = $hongBao;
            $user_hongbao_list->createTime = $now;
            $user_hongbao_list->haoBaoID = $hongBaoID;
            $user_hongbao_list->gameID = $game;
            $user_hongbao_list->sendID = $sendID;
            if($hongBaoID == 0 && $game == 0 && $sendID == 0)
                $user_hongbao_list->followID = 1;
            $user_hongbao_list_result = $user_hongbao_list->save();
            if($user_hongbao_list_result){
                if(!empty($userInfo['openID']) && $hongBaoID == 0){
                    $_hongBao = CommonFunction::formatMoney($hongBao);
                    $content = "恭喜您，获得{$_hongBao}元现金红包（微信首次关注专享）";
                    $desc && $content = $desc;
                    SendWxMessage::hongbao($userInfo['openID'], $_hongBao.'元',  $content);
                }
                return true;
            }else
                return false;
        }else
            return $hongbao_result;


    }


    /*
    * 获得红包
    */
    public static function get_hb($param){

        $userID = $param['userID'];
        $hongBao = $param['hongbao'];
        $now = time();
        $user_hongbao = UserHongbao::findOne($userID);
        if($user_hongbao){
            $user_hongbao->money = $user_hongbao['money'] + $hongBao;
            $user_hongbao->updateTime = $now;
        }else{
            $user_hongbao = new UserHongbao();
            $user_hongbao->money = $hongBao;
            $user_hongbao->updateTime = $now;
            $user_hongbao->userID = $userID;
        }
        $hongbao_result = $user_hongbao->save();
        if($hongbao_result){
            $user_hongbao_list = new UserHongbaoList();
            $user_hongbao_list->userID = $userID;
            $user_hongbao_list->money = $hongBao;
            $user_hongbao_list->createTime = $now;
            $user_hongbao_list->activeID = $param['actID'];
            $user_hongbao_list_result = $user_hongbao_list->save();
            if($user_hongbao_list_result){
                if(!empty($param['openID'])){
                    $_hongBao = CommonFunction::formatMoney($hongBao);
                    SendWxMessage::hongbao($param['openID'], $_hongBao.'元',  $param['desc']);
                }
                return $user_hongbao_list->id;
            }else
                return false;
        }else
            return false;


    }
}